package com.example.bootdemo.utils;


import com.alibaba.excel.util.StringUtils;
import com.example.bootdemo.common.enums.ResultCodeEnum;
import com.example.bootdemo.common.vo.ResultInfoVO;

/**
 * 公共返回对象工具类
 */
public class ResultInfoVOUtil {

    /**
     * 请求出错返回
     *
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> buildError() {
        ResultInfoVO<T> resultInfoVO = build(ResultCodeEnum.FAIL,  null,null);
        return resultInfoVO;
    }

    /**
     * 请求出错返回
     *
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> buildError(String msg) {
        ResultInfoVO<T> resultInfoVO = build(ResultCodeEnum.FAIL,  null,msg);
        return resultInfoVO;
    }

    /**
     * 请求出错返回
     *
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> buildError(ResultCodeEnum resultCodeEnum) {
        ResultInfoVO<T> resultInfoVO = build(resultCodeEnum,  null,null);
        return resultInfoVO;
    }

    /**
     * 请求成功返回
     *
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> buildSuccess() {
        ResultInfoVO<T> resultInfoVO = build(ResultCodeEnum.SUCCESS,  null,null);
        return resultInfoVO;
    }

    /**
     * 请求成功返回
     *
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> buildSuccess(ResultCodeEnum resultCodeEnum) {
        ResultInfoVO<T> resultInfoVO = build(resultCodeEnum,  null,null);
        return resultInfoVO;
    }

    /**
     * 请求成功返回
     *
     * @param data 返回数据对象
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> buildSuccess(T data) {
        ResultInfoVO<T> resultInfoVO = build(ResultCodeEnum.SUCCESS,data,null);
        return resultInfoVO;
    }

    /**
     * 构建返回对象方法
     *

     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultInfoVO<T> build(ResultCodeEnum resultCodeEnum, T data, String msg) {
        if (resultCodeEnum  == null) {
            resultCodeEnum = ResultCodeEnum.SUCCESS;
        }

        ResultInfoVO resultInfoVO = new ResultInfoVO();
        resultInfoVO.setCode(resultCodeEnum.getCode());
        if (StringUtils.isNotBlank(msg)){
            resultInfoVO.setMessage(msg);
        }else{
            resultInfoVO.setMessage(resultCodeEnum.getMessage());
        }
        resultInfoVO.setData(data);
        resultInfoVO.setSuccess(resultCodeEnum == ResultCodeEnum.SUCCESS);
        return resultInfoVO;
    }

}